﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Xml;

namespace YArchitech.HVAC
{
	public partial class hyFrmEdit : System.Windows.Forms.Form
	{
		public hyFrmEdit()
		{
			this.InitializeComponent();
		}

		private void hyFrmEdit_Load(object sender, EventArgs e)
		{
			try
			{
				this.SetDropDownList();
				this.BindData();
				this.hyLoadFilter();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
		}

		private void SetDropDownList()
		{
			this.cboxAttr1.DropDownStyle = ComboBoxStyle.DropDownList;
			this.cboxAttr2.DropDownStyle = ComboBoxStyle.DropDownList;
			this.cboxAttr3.DropDownStyle = ComboBoxStyle.DropDownList;
			this.cboxAttr4.DropDownStyle = ComboBoxStyle.DropDownList;
			this.cboxFormula1.DropDownStyle = ComboBoxStyle.DropDownList;
			this.cboxFormula2.DropDownStyle = ComboBoxStyle.DropDownList;
			this.cboxFormula3.DropDownStyle = ComboBoxStyle.DropDownList;
			this.cboxFormula4.DropDownStyle = ComboBoxStyle.DropDownList;
		}

		private void BindData()
		{
			XmlNode xmlNode = XmlInfo.hyXmlDoc.SelectSingleNode("SelSets/SelSet[@Select='true']");
			this.strCurrName = xmlNode.Attributes["Text"].Value;
			this.txtText.Text = xmlNode.Attributes["Text"].Value;
			this.txtDes.Text = xmlNode.Attributes["Des"].Value;
			foreach (object obj in xmlNode.SelectNodes("Cates/Cate"))
			{
				XmlNode xmlNode2 = (XmlNode)obj;
				string text;
				if (xmlNode2.InnerText == "风口")
				{
					text = "风口，散流器";
				}
				else if (xmlNode2.InnerText == "机械设备")
				{
					text = "风机盘管";
				}
				else
				{
					text = xmlNode2.InnerText;
				}
				if (text != "")
				{
					for (int i = 0; i < this.checkedListBox1.Items.Count; i++)
					{
						if (this.checkedListBox1.Items[i].ToString() == text)
						{
							this.checkedListBox1.SetItemChecked(i, true);
						}
					}
					for (int j = 0; j < this.checkedListBox2.Items.Count; j++)
					{
						if (this.checkedListBox2.Items[j].ToString() == text)
						{
							this.checkedListBox2.SetItemChecked(j, true);
						}
					}
					for (int k = 0; k < this.checkedListBox3.Items.Count; k++)
					{
						if (this.checkedListBox3.Items[k].ToString() == text)
						{
							this.checkedListBox3.SetItemChecked(k, true);
						}
					}
				}
			}
		}

		private List<string> GetAttrs()
		{
			List<string> list = new List<string>();
			for (int i = 0; i < this.checkedListBox1.Items.Count; i++)
			{
				if (this.checkedListBox1.GetItemChecked(i))
				{
					string itemText = this.checkedListBox1.GetItemText(this.checkedListBox1.Items[i]);
					if (itemText == "风口，散流器")
					{
						IEnumerator enumerator = XmlInfo.hyXmlDoc.SelectNodes("SelSets/Config/Cate[@Name='风口']/Attr").GetEnumerator();
						{
							while (enumerator.MoveNext())
							{
								object obj = enumerator.Current;
								XmlNode xmlNode = (XmlNode)obj;
								if (!list.Contains(xmlNode.InnerText))
								{
									list.Add(xmlNode.InnerText);
								}
							}
							goto IL_17D;
						}
					}
					if (itemText == "风机盘管")
					{
						IEnumerator enumerator = XmlInfo.hyXmlDoc.SelectNodes("SelSets/Config/Cate[@Name='机械设备']/Attr").GetEnumerator();
						{
							while (enumerator.MoveNext())
							{
								object obj2 = enumerator.Current;
								XmlNode xmlNode2 = (XmlNode)obj2;
								if (!list.Contains(xmlNode2.InnerText))
								{
									list.Add(xmlNode2.InnerText);
								}
							}
							goto IL_17D;
						}
					}
					foreach (object obj3 in XmlInfo.hyXmlDoc.SelectNodes("SelSets/Config/Cate[@Name='" + itemText + "']/Attr"))
					{
						XmlNode xmlNode3 = (XmlNode)obj3;
						if (!list.Contains(xmlNode3.InnerText))
						{
							list.Add(xmlNode3.InnerText);
						}
					}
				}
				IL_17D:;
			}
			for (int j = 0; j < this.checkedListBox2.Items.Count; j++)
			{
				if (this.checkedListBox2.GetItemChecked(j))
				{
					string itemText2 = this.checkedListBox2.GetItemText(this.checkedListBox2.Items[j]);
					foreach (object obj4 in XmlInfo.hyXmlDoc.SelectNodes("SelSets/Config/Cate[@Name='" + itemText2 + "']/Attr"))
					{
						XmlNode xmlNode4 = (XmlNode)obj4;
						if (!list.Contains(xmlNode4.InnerText))
						{
							list.Add(xmlNode4.InnerText);
						}
					}
				}
			}
			for (int k = 0; k < this.checkedListBox3.Items.Count; k++)
			{
				if (this.checkedListBox3.GetItemChecked(k))
				{
					string itemText3 = this.checkedListBox3.GetItemText(this.checkedListBox3.Items[k]);
					foreach (object obj5 in XmlInfo.hyXmlDoc.SelectNodes("SelSets/Config/Cate[@Name='" + itemText3 + "']/Attr"))
					{
						XmlNode xmlNode5 = (XmlNode)obj5;
						if (!list.Contains(xmlNode5.InnerText))
						{
							list.Add(xmlNode5.InnerText);
						}
					}
				}
			}
			return (from s in list
			orderby s
			select s).ToList<string>();
		}

		private void hyLoadFilter()
		{
			foreach (string item in this.GetAttrs())
			{
				this.cboxAttr1.Items.Add(item);
				this.cboxAttr2.Items.Add(item);
				this.cboxAttr3.Items.Add(item);
				this.cboxAttr4.Items.Add(item);
			}
			XmlNode xmlNode = XmlInfo.hyXmlDoc.SelectSingleNode("SelSets/SelSet[@Select='true']/Filters");
			if (xmlNode == null)
			{
				return;
			}
			foreach (object obj in xmlNode.ChildNodes)
			{
				XmlNode xmlNode2 = (XmlNode)obj;
				string value = xmlNode2.Attributes["Name"].Value;
				string value2 = xmlNode2.Attributes["Valid"].Value;
				string value3 = xmlNode2.Attributes["Attr"].Value;
				string value4 = xmlNode2.Attributes["Value"].Value;
				string value5 = xmlNode2.Attributes["Formula"].Value;
				if (value == "过滤器1")
				{
					if (value2 == "true")
					{
						this.cboxAttr1.Text = value3;
						XmlNode xmlNode3 = XmlInfo.hyXmlDoc.SelectSingleNode("SelSets/Config/Cate/Attr[text()='" + value3 + "']");
						if (xmlNode3 != null)
						{
							WinFormTools.BindComboBox(this.cboxFormula1, xmlNode3);
						}
						this.cboxFormula1.Text = value5;
						this.cboxVal1.Text = value4;
					}
				}
				else if (value == "过滤器2")
				{
					if (value2 == "true")
					{
						this.cboxAttr2.Text = value3;
						XmlNode xmlNode4 = XmlInfo.hyXmlDoc.SelectSingleNode("SelSets/Config/Cate/Attr[text()='" + value3 + "']");
						if (xmlNode4 != null)
						{
							WinFormTools.BindComboBox(this.cboxFormula2, xmlNode4);
						}
						this.cboxFormula2.Text = value5;
						this.cboxVal2.Text = value4;
					}
				}
				else if (value == "过滤器3")
				{
					if (value2 == "true")
					{
						this.cboxAttr3.Text = value3;
						XmlNode xmlNode5 = XmlInfo.hyXmlDoc.SelectSingleNode("SelSets/Config/Cate/Attr[text()='" + value3 + "']");
						if (xmlNode5 != null)
						{
							WinFormTools.BindComboBox(this.cboxFormula3, xmlNode5);
						}
						this.cboxFormula3.Text = value5;
						this.cboxVal3.Text = value4;
					}
				}
				else if (value == "过滤器4" && value2 == "true")
				{
					this.cboxAttr4.Text = value3;
					XmlNode xmlNode6 = XmlInfo.hyXmlDoc.SelectSingleNode("SelSets/Config/Cate/Attr[text()='" + value3 + "']");
					if (xmlNode6 != null)
					{
						WinFormTools.BindComboBox(this.cboxFormula4, xmlNode6);
					}
					this.cboxFormula4.Text = value5;
					this.cboxVal4.Text = value4;
				}
			}
		}

		private void cboxAttr1_SelectedIndexChanged(object sender, EventArgs e)
		{
			try
			{
				XmlNode xmlNode = XmlInfo.hyXmlDoc.SelectSingleNode("SelSets/Config/Cate/Attr[text()='" + this.cboxAttr1.SelectedItem.ToString() + "']");
				if (xmlNode != null)
				{
					WinFormTools.BindComboBox(this.cboxFormula1, xmlNode);
				}
				this.cboxVal1.Items.Clear();
				XmlInfo.GetAttrVal(this.cboxAttr1.Text, this.cboxVal1);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
		}

		private void cboxAttr2_SelectedIndexChanged(object sender, EventArgs e)
		{
			try
			{
				this.cboxFormula2.Items.Clear();
				XmlNode xmlNode = XmlInfo.hyXmlDoc.SelectSingleNode("SelSets/Config/Cate/Attr[text()='" + this.cboxAttr2.SelectedItem.ToString() + "']");
				if (xmlNode != null)
				{
					WinFormTools.BindComboBox(this.cboxFormula2, xmlNode);
				}
				this.cboxVal2.Items.Clear();
				XmlInfo.GetAttrVal(this.cboxAttr2.Text, this.cboxVal2);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
		}

		private void cboxAttr3_SelectedIndexChanged(object sender, EventArgs e)
		{
			try
			{
				this.cboxFormula3.Items.Clear();
				XmlNode xmlNode = XmlInfo.hyXmlDoc.SelectSingleNode("SelSets/Config/Cate/Attr[text()='" + this.cboxAttr3.SelectedItem.ToString() + "']");
				if (xmlNode != null)
				{
					WinFormTools.BindComboBox(this.cboxFormula3, xmlNode);
				}
				this.cboxVal3.Items.Clear();
				XmlInfo.GetAttrVal(this.cboxAttr3.Text, this.cboxVal3);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
		}

		private void cboxAttr4_SelectedIndexChanged(object sender, EventArgs e)
		{
			try
			{
				this.cboxFormula4.Items.Clear();
				XmlNode xmlNode = XmlInfo.hyXmlDoc.SelectSingleNode("SelSets/Config/Cate/Attr[text()='" + this.cboxAttr4.SelectedItem.ToString() + "']");
				if (xmlNode != null)
				{
					WinFormTools.BindComboBox(this.cboxFormula4, xmlNode);
				}
				this.cboxVal4.Items.Clear();
				XmlInfo.GetAttrVal(this.cboxAttr4.Text, this.cboxVal4);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			try
			{
				if (this.txtText.Text.Trim() == "")
				{
					MessageBox.Show("请输入选择集名称。");
					this.tabControl1.SelectedIndex = 0;
				}
				else if (WinFormTools.IsHaveUseless(this.txtText.Text))
				{
					MessageBox.Show("选择集名称不可包含非法字符。", "盈建科提示");
					this.tabControl1.SelectedIndex = 0;
				}
				else if (XmlInfo.SelNameIsExist(this.txtText.Text) && this.strCurrName != this.txtText.Text)
				{
					MessageBox.Show("选择集名称已经存在。", "盈建科提示");
					this.tabControl1.SelectedIndex = 0;
				}
				else if (!WinFormTools.CheckedListBoxIsCheck(this.checkedListBox1) && !WinFormTools.CheckedListBoxIsCheck(this.checkedListBox2) && !WinFormTools.CheckedListBoxIsCheck(this.checkedListBox3))
				{
					MessageBox.Show("请选择类别", "盈建科提示");
					this.tabControl1.SelectedIndex = 1;
				}
				else
				{
					XmlNode xmlNode = XmlInfo.hyXmlDoc.SelectSingleNode("SelSets/SelSet[@Select='true']");
					xmlNode.Attributes["Text"].Value = this.txtText.Text;
					xmlNode.Attributes["Des"].Value = this.txtDes.Text;
					XmlNode xmlNode2 = xmlNode.SelectSingleNode("Cates");
					xmlNode2.RemoveAll();
					for (int i = 0; i < this.checkedListBox1.CheckedItems.Count; i++)
					{
						XmlElement xmlElement = XmlInfo.hyXmlDoc.CreateElement("Cate");
						if ("风口，散流器" == this.checkedListBox1.CheckedItems[i].ToString())
						{
							xmlElement.InnerText = "风口";
						}
						else if ("风机盘管" == this.checkedListBox1.CheckedItems[i].ToString())
						{
							xmlElement.InnerText = "机械设备";
						}
						else
						{
							xmlElement.InnerText = this.checkedListBox1.CheckedItems[i].ToString();
						}
						xmlNode2.AppendChild(xmlElement);
					}
					for (int j = 0; j < this.checkedListBox2.CheckedItems.Count; j++)
					{
						XmlElement xmlElement2 = XmlInfo.hyXmlDoc.CreateElement("Cate");
						xmlElement2.InnerText = this.checkedListBox2.CheckedItems[j].ToString();
						xmlNode2.AppendChild(xmlElement2);
					}
					for (int k = 0; k < this.checkedListBox3.CheckedItems.Count; k++)
					{
						XmlElement xmlElement3 = XmlInfo.hyXmlDoc.CreateElement("Cate");
						xmlElement3.InnerText = this.checkedListBox3.CheckedItems[k].ToString();
						xmlNode2.AppendChild(xmlElement3);
					}
					xmlNode.AppendChild(xmlNode2);
					XmlNode xmlNode3 = xmlNode.SelectSingleNode("Filters");
					xmlNode3.RemoveAll();
					if (this.cboxVal1.Text.Trim() != "" && this.cboxAttr1.Text != "" && this.cboxFormula1.Text != "")
					{
						XmlInfo.UpdateFilter(xmlNode, xmlNode3, "过滤器1", "true", this.cboxAttr1.Text, this.cboxVal1.Text, this.cboxFormula1.Text);
					}
					else
					{
						XmlInfo.UpdateFilter(xmlNode, xmlNode3, "过滤器1", "false", "", "", "");
					}
					if (this.cboxVal2.Text.Trim() != "" && this.cboxAttr2.Text != "" && this.cboxFormula2.Text != "")
					{
						XmlInfo.UpdateFilter(xmlNode, xmlNode3, "过滤器2", "true", this.cboxAttr2.Text, this.cboxVal2.Text, this.cboxFormula2.Text);
					}
					else
					{
						XmlInfo.UpdateFilter(xmlNode, xmlNode3, "过滤器2", "false", "", "", "");
					}
					if (this.cboxVal3.Text.Trim() != "" && this.cboxAttr3.Text != "" && this.cboxFormula3.Text != "")
					{
						XmlInfo.UpdateFilter(xmlNode, xmlNode3, "过滤器3", "true", this.cboxAttr3.Text, this.cboxVal3.Text, this.cboxFormula3.Text);
					}
					else
					{
						XmlInfo.UpdateFilter(xmlNode, xmlNode3, "过滤器3", "false", "", "", "");
					}
					if (this.cboxVal4.Text.Trim() != "" && this.cboxAttr4.Text != "" && this.cboxFormula4.Text != "")
					{
						XmlInfo.UpdateFilter(xmlNode, xmlNode3, "过滤器4", "true", this.cboxAttr4.Text, this.cboxVal4.Text, this.cboxFormula4.Text);
					}
					else
					{
						XmlInfo.UpdateFilter(xmlNode, xmlNode3, "过滤器4", "false", "", "", "");
					}
					base.DialogResult = DialogResult.OK;
				}
			}
			catch (UnauthorizedAccessException ex)
			{
				ex.ToString();
				MessageBox.Show("请以管理员身份运行。");
			}
			catch (Exception ex2)
			{
				MessageBox.Show(ex2.ToString());
			}
		}

		private void BindFilters()
		{
			this.cboxAttr1.Items.Clear();
			this.cboxAttr2.Items.Clear();
			this.cboxAttr3.Items.Clear();
			this.cboxAttr4.Items.Clear();
			foreach (string item in this.GetAttrs())
			{
				this.cboxAttr1.Items.Add(item);
				this.cboxAttr2.Items.Add(item);
				this.cboxAttr3.Items.Add(item);
				this.cboxAttr4.Items.Add(item);
			}
		}

		private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
		{
			try
			{
				this.CheckListChange();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
		}

		private void CheckListChange()
		{
			this.BindFilters();
			this.cboxAttr1.Text = "";
			this.cboxAttr2.Text = "";
			this.cboxAttr3.Text = "";
			this.cboxAttr4.Text = "";
			this.cboxFormula1.Items.Clear();
			this.cboxFormula2.Items.Clear();
			this.cboxFormula3.Items.Clear();
			this.cboxFormula4.Items.Clear();
			this.cboxFormula1.Text = "";
			this.cboxFormula2.Text = "";
			this.cboxFormula3.Text = "";
			this.cboxFormula4.Text = "";
			this.cboxVal1.Items.Clear();
			this.cboxVal2.Items.Clear();
			this.cboxVal3.Items.Clear();
			this.cboxVal4.Items.Clear();
			this.cboxVal1.Text = "";
			this.cboxVal2.Text = "";
			this.cboxVal3.Text = "";
			this.cboxVal4.Text = "";
		}

		private void hyFrmEdit_FormClosing(object sender, FormClosingEventArgs e)
		{
		}

		private void checkedListBox2_SelectedIndexChanged(object sender, EventArgs e)
		{
			try
			{
				this.CheckListChange();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void checkedListBox3_SelectedIndexChanged(object sender, EventArgs e)
		{
			try
			{
				this.CheckListChange();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
		}

		private void txtText_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (WinFormTools.IsHaveUseless(e.KeyChar.ToString()))
			{
				e.Handled = true;
				return;
			}
			e.Handled = false;
		}

		private string strCurrName = "";
	}
}
